# Maintainer: Alexey Pavlov <Alexpux@gmail.com>
# Contributor: Martell Malone <martellmalone@gmail.com>
# Contributor: Renato Silva <br.renatosilva@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

_realname=zlib
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-minizip")
pkgver=1.2.13
pkgrel=2
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
license=('spdx:Zlib')
url="https://www.zlib.net/"
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-bzip2")
source=("https://zlib.net/current/${_realname}-${pkgver}.tar.gz"
        01-zlib-1.2.11-1-buildsys.mingw.patch
        03-dont-put-sodir-into-L.mingw.patch
        04-fix-largefile-support.patch
        06-add-64bit-functions-and-fix-typos.patch
        07-Add-no-undefined-to-link-to-enable-build-shared-vers.patch
        08-Add-bzip2-library-to-pkg-config-file.patch)
sha256sums=('b3a24de97a8fdbc835b9833169501030b8977031bcb54b3b3ac13740f846ab30'
            '8175e70c980ffc48a2e90c6f0431ddb80ef6ff690f9e8906dd6a756238552fa5'
            '7287d12db57dcf0f66964c861100bf06ebe1ddcb243e7ee0773fcab1b2935596'
            '4d8ebda55b95d19cd7c467c486d4c42d2bad6db9ff5dca361f6d8c24120e5f30'
            '7f84be94607ab9e97ed5be2a026f9e0fb8080c3b75ba42fe7a9021a1198f563d'
            'fb83292f494f649ea7f1835aa4abea61acc593d7f90625741d42cd99cac0e075'
            '0be98a7e660e7c068856f08b9e92d6c73a6b0d88c3e29a9716140b147f7c23c5')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd ${srcdir}/${_realname}-${pkgver}
  apply_patch_with_msg \
    01-zlib-1.2.11-1-buildsys.mingw.patch \
    03-dont-put-sodir-into-L.mingw.patch \
    04-fix-largefile-support.patch

  cd ${srcdir}/${_realname}-${pkgver}/contrib/minizip
  apply_patch_with_msg \
    06-add-64bit-functions-and-fix-typos.patch \
    07-Add-no-undefined-to-link-to-enable-build-shared-vers.patch \
    08-Add-bzip2-library-to-pkg-config-file.patch
  autoreconf -fi
}

build() {
  [[ -d "${srcdir}/build-${MSYSTEM}" ]] && rm -rf "${srcdir}/build-${MSYSTEM}"
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  CHOST=${MINGW_CHOST} \
  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --shared

  make

  # build minizip
  [[ -d "${srcdir}/build-mz-${MSYSTEM}" ]] && rm -rf "${srcdir}/build-mz-${MSYSTEM}"
  mkdir -p "${srcdir}/build-mz-${MSYSTEM}" && cd "${srcdir}/build-mz-${MSYSTEM}"

  CFLAGS+=" -DHAVE_BZIP2"
  ../${_realname}-${pkgver}/contrib/minizip/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --enable-demos \
    LIBS="-lbz2"

  make
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"
  make test
}

package_zlib() {
  pkgdesc="Compression library implementing the deflate compression method found in gzip and PKZIP (mingw-w64)"

  cd "${srcdir}/build-${MSYSTEM}"

  make DESTDIR="${pkgdir}" install

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}

package_minizip() {
  pkgdesc="Mini zip and unzip based on zlib (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-zlib"
           "${MINGW_PACKAGE_PREFIX}-bzip2")
  conflicts=("${MINGW_PACKAGE_PREFIX}-minizip-git")
  replaces=("${MINGW_PACKAGE_PREFIX}-minizip-git")
  provides=("${MINGW_PACKAGE_PREFIX}-minizip-git")

  cd "${srcdir}/build-mz-${MSYSTEM}"

  make DESTDIR="${pkgdir}" install

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/minizip/LICENSE"

  # https://github.com/madler/zlib/pull/229
	rm "${pkgdir}${MINGW_PREFIX}/include/minizip/crypt.h"
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
